package com.example.testanimation.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nova.scenic.projectlibs.util.ListActivity;
import com.scenic.rd_android.R;

import java.util.ArrayList;
import java.util.List;

public class TestListView extends ListActivity {

	private MyAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		adapter = new MyAdapter();
		setListAdapter(adapter);
		
		ArrayList<MyEntity> data = new ArrayList<MyEntity>();
		
		
		for(int i = 0 ; i < 100; i++){
			MyEntity entity = new MyEntity();
			entity.name = "\t" + i;
			if(i < 3){
				entity.type = 0;
			}else{
				entity.type = 1;
			}
			data.add(entity);
		}
		{
			MyEntity temp = new MyEntity();
			temp.type = 0;
			temp.name = "其他设备";
			data.add(3, temp);
			
		}
		{
			MyEntity temp = new MyEntity();
			temp.type = 0;
			temp.name = "我的设备";
			data.add(0, temp);
			
		}
		
		
		adapter.notifyDataSetChanged(data);
		
		getListView().getLayoutParams().height = 700;
		
		getListView().setDividerHeight(20);
	}
	
	
	class MyAdapter extends BaseAdapter {
		
		List<MyEntity> data = new ArrayList<MyEntity>();
		
		public void notifyDataSetChanged(List<MyEntity> data) {
			this.data.clear();
			this.data.addAll(data);
			
			notifyDataSetChanged();
			
		}
		
		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public int getViewTypeCount() {
			return 2;
		}
		
		@Override
		public int getItemViewType(int position) {
			
			return data.get(position).type;
		}
		
		@Override
		public boolean isEnabled(int position) {
			return super.isEnabled(position);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MyEntity entity = data.get(position);
			
			switch(entity.type){
			case 0: 
				if(convertView == null){
					convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test_listview_layout_1, null);
				}
				break;
			case 1:
				if(convertView == null){
					convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test_listview_layout_2, null);
				}
				break;
			}
			TextView txView = (TextView) convertView.findViewById(R.id.id_txview_1);
			txView.setText(Integer.toHexString(convertView.hashCode()) + " \t "  + entity.name);
			
			return convertView;
		}
		
	}
	
	
	class MyEntity {
		int type;
		String name;
		Object blue;
		
	}
	
	
}
